package com.example.networkrequestencapsulation;

import android.Manifest;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull;
import butterknife.BindView;
import com.example.networkrequestencapsulation.base.BaseActivity;
import com.example.networkrequestencapsulation.callback.DialogCallback;
import com.example.networkrequestencapsulation.network.ApiUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.lzy.okgo.model.Response;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

import java.util.List;

/**
 * 作者 : chenyixi
 * 日期 : 2019/11/19 18:35
 * 文件包名 : com.example.networkrequestencapsulation
 * 描述 :
 */
public class MainActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks, EasyPermissions.RationaleCallbacks {

    private static final int RC_CAMERA_PERM = 123;
    @BindView(R.id.btn)
    Button btn;
    private String[] perms = {
            Manifest.permission.CAMERA
    };

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initData() {
        //相机权限请求
        cameraTask();
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .statusBarColor(R.color.colorPrimary)
                .navigationBarColor(R.color.colorPrimary)
                .init();
    }

    @Override
    protected void initListener() {
        //测试网络请求
        btn.setOnClickListener(view -> ApiUtils.getInstance().getTOH("57e8ec2c8e96c55445632e8a5b8f65c0",
                11, 20, new DialogCallback(mActivity) {
                    @Override
                    public void onSuccess(Response response) {
//
                    }
                }));
    }


    @AfterPermissionGranted(RC_CAMERA_PERM)
    public void cameraTask() {
        if (EasyPermissions.hasPermissions(this, perms)) {
            // Have permission, do the thing!
            Toast.makeText(this, "TODO: Camera things", Toast.LENGTH_LONG).show();
        } else {
            // Ask for one permission
            EasyPermissions.requestPermissions(
                    this,
                    "本功能需求请求权限",
                    RC_CAMERA_PERM,
                    Manifest.permission.CAMERA);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        // EasyPermissions handles the request result.
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {

        // (Optional) Check whether the user denied any permissions and checked "NEVER ASK AGAIN."
        // This will display a dialog directing them to enable the permission in app settings.
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            new AppSettingsDialog.Builder(this)
                    .setTitle("权限设置")
                    .setRationale("您未允许获取定位权限，您可在系统设置中开启")
                    .setPositiveButton("去设置")
                    .setNegativeButton("暂不")
                    .build().show();
        }
    }


    @Override
    public void onRationaleAccepted(int requestCode) {

    }

    @Override
    public void onRationaleDenied(int requestCode) {

    }
}
